package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.domain.dto.fileConfig.FileConfigQueryDTO;
import com.sh.rbac.domain.dto.fileConfig.FileConfigSaveDTO;
import com.sh.rbac.domain.dto.fileConfig.FileConfigUpdateDTO;
import com.sh.rbac.domain.entity.FileConfig;
import com.sh.rbac.service.FileConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 存储配置控制器
 *
 * @author lucky
 * @since 2024-06-26
 */

@Tag(name = "存储配置管理", description = "存储配置相关操作")
@RequestMapping("sys/file-config")
@RestController
public class FileConfigController {

    @Autowired
    private FileConfigService fileConfigService;


    @Operation(summary = "分页查询存储配置列表", description = "根据存储配置条件查询存储配置进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = FileConfig.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:file-config:page')")
    public Result<Page<FileConfig>> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody FileConfigQueryDTO fileConfigQueryDTO) {
        return Result.success(fileConfigService.getPage(current, limit, fileConfigQueryDTO));
    }

    @Operation(summary = "根据ID查询存储配置")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = FileConfig.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("@ps.hasPerm('sys:file-config:get')")
    public Result<?> get(@PathVariable Long id) {
        return Result.success(fileConfigService.getById(id));
    }



    @SysLog(module = "系统管理->存储配置管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加存储配置", description = "保存一个新存储配置")
    @ApiResponse(responseCode = "200", description = "成功保存存储配置")
    @PostMapping("save")
    @PreAuthorize("@ps.hasPerm('sys:file-config:save')")
    public Result<Boolean> save(@RequestBody FileConfigSaveDTO fileConfigSaveDTO) {
        return fileConfigService.save(fileConfigSaveDTO) ? Result.success() : Result.failure();
    }


    @SysLog(module = "系统管理->存储配置管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新存储配置", description = "根据ID更新存储配置信息")
    @ApiResponse(responseCode = "200", description = "成功更新存储配置")
    @PutMapping("update")
    @PreAuthorize("@ps.hasPerm('sys:file-config:update')")
    public Result<Boolean> update(@RequestBody FileConfigUpdateDTO fileConfigUpdateDTO) {
        return fileConfigService.updateById(fileConfigUpdateDTO) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->存储配置管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除存储配置", description = "根据ID删除存储配置信息")
    @ApiResponse(responseCode = "200", description = "成功删除存储配置")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("@ps.hasPerm('sys:file-config:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return fileConfigService.removeById(id) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->存储配置管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除存储配置", description = "批量删除存储配置信息")
    @ApiResponse(responseCode = "200", description = "成功删除存储配置")
    @DeleteMapping("batch-delete")
    @PreAuthorize("@ps.hasPerm('sys:file-config:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return fileConfigService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->存储配置管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新存储配置状态", description = "根据ID更新存储配置状态")
    @ApiResponse(responseCode = "200", description = "成功更新存储配置状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("@ps.hasPerm('sys:file-config:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return fileConfigService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
